package jm.demo.spring.el;

import java.util.GregorianCalendar;
import java.util.HashMap;

import jm.demo.spring.el.bean.Inventor;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.StopWatch;

public class ElDemo {
	
	
	public static void testStaticMethod() {
		ExpressionParser parser = new SpelExpressionParser();

		// invokes 'getBytes()'
		// Expression exp = parser.parseExpression("#{T(java.lang.System).currentTimeMillis()}", new TemplateParserContext());
		
		Expression exp = parser.parseExpression("T(java.lang.System).currentTimeMillis()");
		
		StopWatch stopWatch = new StopWatch();
		exp.getValue();
		
		
		long counter = 1000;

		stopWatch.start("preheat - " + counter);
		
		for(int i = 0; i < counter; i++) {
			exp.getValue();
		}
		stopWatch.stop();
		
		counter = 1000000;
		stopWatch.start("real - " + counter);
		for(int i = 0; i < counter; i++) {
			exp.getValue(Long.class);
		}
		stopWatch.stop();
		
		System.out.println(stopWatch.prettyPrint());

		System.out.println(exp.getValue());
		
		try {
			Thread.sleep(1000L);
		} catch (InterruptedException e) {}
		
		System.out.println(exp.getValue());
		
		
		
	}
	
	public static void testSimpleBean() {
		
		ExpressionParser parser = new SpelExpressionParser();
		GregorianCalendar c = new GregorianCalendar();
		c.set(1856, 7, 9);

		//  The constructor arguments are name, birthday, and nationality.
		Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");

		Expression exp1 = parser.parseExpression("name");
		EvaluationContext context = new StandardEvaluationContext(tesla);

		long counter = 1000;
		
		StopWatch stopWatch = new StopWatch();
		stopWatch.start("preheat");
		
		for(int i = 0; i < counter; i++) {
			Object value = exp1.getValue(context);
			System.out.println(value);
		}
		stopWatch.stop();
		

		counter = 1000000;
		stopWatch.start("real");
		for(int i = 0; i < counter; i++) {
			exp1.getValue(context, String.class);
		}
		stopWatch.stop();
		
		System.out.println(stopWatch.prettyPrint());
		
		
		String name = (String) exp1.getValue(context);
		System.out.println(name);
	}

    public static void testSimpleExpression() {
        
        ExpressionParser parser = new SpelExpressionParser();
        GregorianCalendar c = new GregorianCalendar();
        c.set(1856, 7, 9);

        //  The constructor arguments are name, birthday, and nationality.
        HashMap<String, Object> map = new HashMap<String, Object>();

        map.put("counter", 1);
        // Expression exp1 = parser.parseExpression("(#map).get('counter')");
        Expression exp1 = parser.parseExpression("(#counter)+1");
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("map", map);
        context.setVariable("counter", 1);

        long counter = 1000;
        
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("preheat");
        
        for(int i = 0; i < counter; i++) {
            Object value = exp1.getValue(context);
            System.out.println(value);
        }
        stopWatch.stop();
        

        counter = 1000000;
        stopWatch.start("real");
        for(int i = 0; i < counter; i++) {
            exp1.getValue(context, String.class);
        }
        stopWatch.stop();
        
        System.out.println(stopWatch.prettyPrint());
        
        
        String name = (String) exp1.getValue(context);
        System.out.println(name);
    }

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	    testStaticMethod();
		// testSimpleBean();
	    // testSimpleExpression();

		
	}

}
